package com.inspinia.base.pagination;

import java.util.List;

/**
 * 用于组装和计算list分页的工具
 * zhangyuan on 2017/6/9.
 * @param <T>
 */
public class PageModel<T>  {
    private int pageNo = 1; // 当前页
    public int totalPage = 0; // 总页数
    private int pageSize;// 每页20条数据
    private int totalCount = 0; // 总数据数
    private int pageStartRow = 0;// 每页的起始数
    private int pageEndRow = 0; // 每页显示数据的终止数
    private boolean hasNextPage = false; // 是否有下一页
    private boolean hasPreviousPage = false; // 是否有前一页
    private List<T> list;
    protected String sortBy; //排序字段名称
    protected int offset;
    private String keyWord; //关键字

    /**
     *
     * @param list
     * @param pageRecorders
     */
    public PageModel(List<T> list, int pageRecorders) {
        init(list, pageRecorders);// 通过对象集，记录总数划分
    }

    /** */
    /**
     * 初始化list，并告之该list每页的记录数
     *
     * @param list
     * @param pageRecorders
     */
    public void init(List<T> list, int pageRecorders) {
        this.pageSize = pageRecorders;
        this.list = list;
        totalCount = list.size();

        hasPreviousPage = false;

        if ((totalCount % pageRecorders) == 0) {
            totalPage = totalCount / pageRecorders;
        } else {
            totalPage = totalCount / pageRecorders + 1;
        }

        if (pageNo >= totalPage) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }

        if (totalCount < pageRecorders) {
            this.pageStartRow = 0;
            this.pageEndRow = totalCount;
        } else {
            this.pageStartRow = 0;
            this.pageEndRow = pageRecorders;
        }
    }

    // 判断要不要分页
    public boolean isNext() {
        return list.size() > 20;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    /**
     *
     * @return
     */
    public List<T> getNextPage() {
        pageNo = pageNo + 1;
        disposePage();
        return getObjects(pageNo);
    }

    /**
     * 处理分页
     */
    private void disposePage() {
        if (pageNo == 0) {
            pageNo = 1;
        }
        if ((pageNo - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }

        if (pageNo >= totalPage) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
    }

    /**
     * 上一页
     *
     * @return
     */
    public List<T> getPreviousPage() {
        pageNo = pageNo - 1;

        if ((pageNo - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }
        if (pageNo >= totalPage) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        return getObjects(pageNo);
    }

    /**
     * 获取第几页的内容
     *
     * @param page
     * @return
     */
    public List<T> getObjects(int page) {
        if (page == 0) {
            this.setPage(1);
            page = 1;
        } else {
            this.setPage(page);
        }

        this.disposePage();

        if (page * pageSize < totalCount) {// 判断是否为最后一页
            pageEndRow = page * pageSize;
            pageStartRow = pageEndRow - pageSize;
        } else {
            pageEndRow = totalCount;
            pageStartRow = pageSize * (totalPage - 1);
        }

        List<T> objects = null;
        if (!list.isEmpty()) {
            objects = list.subList(pageStartRow, pageEndRow);
        }
        return objects;
    }

    /**
     * 第一页
     *
     * @return
     */
    public List<T> getFistPage() {
        if (this.isNext()) {
            return list.subList(0, pageSize);
        } else {
            return list;
        }
    }

    /**
     * @return the page
     */
    public int getPage() {
        return pageNo;
    }

    /**
     * @param page
     *            the page to set
     */
    public void setPage(int page) {
        this.pageNo = page;
    }

    /**
     * @return the totalPages
     */
    public int getTotalPage() {
        return totalPage;
    }

    /**
     * @param totalPages
     *            the totalPages to set
     */
    public void setTotalPage(int totalPages) {
        this.totalPage = totalPages;
    }

    /**
     * @return the pageRecorders
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageRecorders
     *            the pageRecorders to set
     */
    public void setPagesize(int pageRecorders) {
        this.pageSize = pageRecorders;
    }

    /**
     * @return the totalRows
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * @param totalRows
     *            the totalRows to set
     */
    public void setTotalCount(int totalRows) {
        this.totalCount = totalRows;
    }

    /**
     * @return the pageStartRow
     */
    public int getPageStartRow() {
        return pageStartRow;
    }

    /**
     * @param pageStartRow
     *            the pageStartRow to set
     */
    public void setPageStartRow(int pageStartRow) {
        this.pageStartRow = pageStartRow;
    }

    /**
     * @return the pageEndRow
     */
    public int getPageEndRow() {
        return pageEndRow;
    }

    /**
     * @param pageEndRow
     *            the pageEndRow to set
     */
    public void setPageEndRow(int pageEndRow) {
        this.pageEndRow = pageEndRow;
    }

    /**
     * @return the hasNextPage
     */
    public boolean isHasNextPage() {
        return hasNextPage;
    }

    /**
     * @param hasNextPage
     *            the hasNextPage to set
     */
    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    /**
     * @return the list
     */
    public List<T> getList() {
        return list;
    }

    /**
     * @param list
     *            the list to set
     */
    public void setList(List<T> list) {
        this.list = list;
    }

    /**
     * @return the hasPreviousPage
     */
    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public int getOffset() {
        return (pageNo - 1) * pageSize;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public String getSortBy() {
        return sortBy;
    }

    public void setSortBy(String sortBy) {
        this.sortBy = sortBy;
    }

}